﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Data;
using System.ComponentModel.DataAnnotations;

namespace 控制台应用
{
    public static class EmialHelper
    {
        /// <summary>
        /// 验证邮箱格式是否正确(正则表达式)
        /// </summary>
        /// <param name="strEmail">邮箱</param>
        /// <returns>true:格式正确，否则false</returns>
        public static bool CheckEmailForRegex(string strEmail)
        {
            //Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");            //网上找的    支持中文邮箱
            Regex regex = new Regex(@"^[.a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");         //王继位写的 不支持中文邮箱
            return regex.IsMatch(strEmail);
        }

        /// <summary>
        /// 验证邮箱格式是否正确(EmailAddressAttribute类)
        /// </summary>
        /// <param name="strEmail">邮箱</param>
        /// <returns>true:格式正确，否则false</returns>
        public static bool CheckEmailForEmailAtt(string strEmail)
        {
            EmailAddressAttribute emailatt = new EmailAddressAttribute();
            if (strEmail == null)
            {
                return false;
            }
            return emailatt.IsValid(strEmail);
        }

        /// <summary>
        /// 验证邮箱格式是否正确(mailAddress类)
        /// </summary>
        /// <param name="strEmail">邮箱</param>
        /// <returns>true:格式正确，否则false</returns>
        public static bool CheckEmailForEmailAddress(string strEmail)
        {
            try
            {
                new System.Net.Mail.MailAddress(strEmail);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
